TIL: To add user(s) to an existing schedule, they should be added to a schedule layer

API: https://api-reference.pagerduty.com/#!/Schedules/put_schedules_id
Problem: Add a new user to an existing schedule
Solution: Add them to a schedule_layer
Some code that would be helpful to the community:
indent preformatted text by 4 spaces

getScheduleData = requests.request("GET","api-pagerduty-com/schedules/ABCDXYZ",headers=pagerdutyHeaders)

scheduleData = json.loads(getScheduleData.text)

userList = scheduleData['schedule']['schedule_layer'][0]['users']

new_user = {"user":{"id":"MNOPDEF","type":"user_reference"}}

userList.append(new_user)

scheduleData['schedule']['schedule_layer'][0]['users'] = userList

updateScheduleResponse = requests.request("PUT","api.pagerduty.com/schedules/ABCDXYZ",headers=pagerdutyHeaders,data=json.dumps(scheduleData))
1 Like